#include "xIPC.h"
//#define PIPE_TEST
#ifndef PIPE_TEST
#define ROUTER
//#define CLIENT_1
//#define CLIENT_2
#if ((defined ROUTER) || (defined CLIENT_2))
EErrNo MessageHandler(struct message_listener_t *pMsgListener, TMessage *pMsg)
{
    if (!pMsgListener || !pMsg)
        return -ERRNO_EPARAMETER_INVALID;

    aux_prt_debug("!!!!!!!!!!!!!!!!!!!!!!!!handle one msg, client %s\n", pMsg->szClient);
    aux_prt_err("sender %s, dest %s, content %s\n", pMsg->szSender,
                pMsg->szReceiverFilter,
                pMsg->pData);
    return ERRNO_SUCCESS;
}
#endif

#ifdef CLIENT_1
TMessage tMsg;
#endif

int main(void)
{
    TMessageListener   tListener;
#ifdef ROUTER
    TxIPCMessageRouter tRouter;
#else
    TxIPCMessageClient tClient;
#endif
    xIPC_Init();

    memset(&tListener, 0, sizeof(TMessageListener));
    tListener.eListenFilterType = LISTEN_TYPE_ALL;


#ifdef ROUTER
    memset(&tRouter, 0, sizeof(TxIPCMessageRouter));

    xIPC_Router_Init(&tRouter, "127.0.0.1", 4450,  "router");
    tListener.pMessageHandler   = MessageHandler;
    tListener.uOwner.ptRouter          = &tRouter;
    xIPC_Router_AddListener(&(tRouter), &tListener);

    aux_sleep_second(300);
    xIPC_Router_Deinit(&tRouter);

#else
#ifdef CLIENT_2
    if (ERRNO_SUCCESS == xIPC_Client_Init(&tClient, "127.0.0.1", 4450,
                                        "client_example2"))
    {
        tListener.uOwner.ptClient          = &tClient;
        tListener.pMessageHandler   = MessageHandler;
        xIPC_Client_AddListener(&tClient, &tListener);
    }
#else
    if (ERRNO_SUCCESS == xIPC_Client_Init(&tClient, "127.0.0.1", 4450,
                                        "client_example1"))
    {
        memset(&tMsg, 0, sizeof(tMsg));
        tMsg.eMessageDestinationType = MESSAGE_DST_TYPE_TO_CLIENT;
        tMsg.eMessageId = MESSAGE_ID_CLIENT_SEND_COMMON_MESSAGE_TO_ROUTER;

        tMsg.eReceiverFilterType = RECEIVER_FILTER_NAME;
        strcpy(tMsg.szReceiverFilter, "client_example2");
        xIPC_Client_Send(&tClient, &tMsg);
    }
#endif

    aux_sleep_second(10);
    xIPC_Client_Deinit(&tClient);

#endif
    //
    //

    //aux_sleep_second(100);
    xIPC_Deinit();
    return 0;
}
#else
#define PIPE_RECV
#ifndef PIPE_RECV
int main(void)
{
    TxIPCIOPipe *ptPipe = NULL;

    xIPC_Init();

    ptPipe = xIPC_Pipe_Get("127.0.0.1", 4450, "A", "B");
    if (!ptPipe)
    {
        aux_prt_err("ptPipe is NULL!\n");
        return 0;
    }
    aux_prt_err("!!!!!!!!!!!send!!!!!!!!!!!!!!\n");
    xIPC_Pipe_Send(ptPipe, "B", "testAAAAAAAAA", sizeof("testAAAAAAAAA"));
    aux_sleep_second(100);
    xIPC_Pipe_Put(ptPipe);

    xIPC_Deinit();
    return 0;
}
#else
int main(void)
{
    char buffer[256];

    TxIPCIOPipe *ptPipe = NULL;

    xIPC_Init();

    ptPipe = xIPC_Pipe_Get("127.0.0.1", 4450, "B", "A");
    if (!ptPipe)
    {
        aux_prt_err("ptPipe is NULL!\n");
        return 0;
    }


    memset(buffer, 0, 256);
	//aux_sleep_second(50);
    //while (1)
    {

        int nCnt = xIPC_Pipe_Wait(ptPipe, buffer, 3, 100000);
        if (nCnt > 0)
        {
            aux_prt_err("!!!!!!!!!recv %d bytes,content: %s\n", nCnt, buffer);
            memset(buffer, 0, 256);
        }
        aux_sleep_second(10);
    }

    xIPC_Pipe_Put(ptPipe);

    xIPC_Deinit();
    return 0;
}
#endif
#endif